package tijava.generics;

//: generics/GenericsAndCovariance.java
import java.util.*;

public class GenericsAndCovariance {
	public static void main(String[] args) {
		List<? extends Fruit> aa = new ArrayList<Apple>();

		// Wildcards allow covariance:
		List<? extends Fruit> flist = new ArrayList<Apple>() {
			{
				add(new Apple());
			}
		};
		Fruit f = flist.get(0);
		System.out.println(f);

		List<Fruit> lfruit = new ArrayList<Fruit>();
		lfruit.add(new Jonathan());
		flist = lfruit;
		f = flist.get(0);
		System.out.println(f);

		flist = Arrays.asList(new Jonathan());
		// Compile Error: can't add any type of object:
		// flist.add(new Apple());
		// flist.add(new Fruit());
		// flist.add(new Object());
		// We know that it returns at least Fruit:
		f = flist.get(0);
		System.out.println(f);

		add(fruits, new Apple());
	}

	static List<Fruit> fruits = new ArrayList<Fruit>();

	static <T> void add(List<T> list, T item) {
		list.add(item);
	}
} // /:~
